package kpi.javatraining6.gnatiuk.src.tester.question;

import kpi.javatraining6.gnatiuk.src.teachingsystem.Difficulty;
import kpi.javatraining6.gnatiuk.src.tester.readers.QuestionValues;

import java.util.ArrayList;
import java.util.List;

/**
 * Question object that contains question value, variants of answers and correct
 * answers
 * @author Surp
 */
public abstract class Question {

	public static final int SINGLE_ANSWER_QUESTION=0;
	public static final int MULTIPLE_ANSWER_QUESTION=1;
	public static final int OWN_ANSWER_QUESTION=2;
	private String question;
	private List<String> answers;
	private List<String> correctAnswers;
	private int score;
    private Difficulty difficulty;

	/**
	 * initialize all necessary fields
	 * @param question - question value
	 * @param answers - variants of answers
	 * @param correctAnswers - correct answers
	 */
	public Question(String stringDifficulty, String question, List<String> answers, List<String> correctAnswers) {
        this.difficulty = Difficulty.valueOf(stringDifficulty);
		this.question = question;
		this.answers = answers;
		this.correctAnswers = correctAnswers;
		score = 0;
	}

    public Question(QuestionValues questionValues) {
        this(questionValues.getDifficulty(), questionValues.getQuestion(),
             questionValues.getAnswers(), questionValues.getCorrectAnswers());
        this.score = questionValues.getScore();
    }

    public Question(Question origin) {
        this(origin.difficulty.toString(), origin.question, origin.answers, origin.correctAnswers);
    }


    public String getTopic() {
        return "Тема не определена";
    }

	/**
	 * determine question note according to concrete type of a question
	 */
	abstract protected String determineQuestionNote();


    public Difficulty getDifficulty() {
        return difficulty;
    }

	public boolean answerTheQuestion(ArrayList<String> answerArray){
        boolean answered = false;
		for (String corrAnswers : correctAnswers) {
            answered = false;
			for (String answer : answerArray) {
				if(isCorrectAnswered(corrAnswers,answer)){
					answered = true;
					break;
				}
			}
			if (!answered) {
				return answered;
			}
		}
		
		if (correctAnswers.size() == answerArray.size()) {
			score = 1;
		}

        return answered;
	}


	/**
	 * check if answer is correct
	 */
	protected boolean isCorrectAnswered(String correctAnswer, String userAnswer){
        return correctAnswer.equals(userAnswer);
    }

    @Override
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof Question) {
            Question someQuestion = (Question)anObject;
            return this.question.equals(someQuestion.question);
        }
        return false;
    }
	
	@Override
	public String toString() {
		String question=String.format("%s (%s)\n",this.question, determineQuestionNote());
        for (int i = 0; i < answers.size(); i++) { 
            question+=String.format("%d. %s \n", (i + 1),answers.get(i));
        } 
		return question;
	}

    public String getQuestion() {
        return question;
    }
}
